package com.hunan.api.catv.service.impl.tencent_cloud.vm;

import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.utils.GsonUtils;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.Region;
import com.tencentcloudapi.vm.v20200709.VmClient;
import com.tencentcloudapi.vm.v20200709.models.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VmService {

    public static final Logger LOGGER = LoggerFactory.getLogger(VmService.class);

    @Autowired
    private CacheBiz cacheBiz;

    public CreateVideoModerationTaskResponse createVideoModerationTask(String url, String videoPrimaryKey) {
        CreateVideoModerationTaskResponse response = null;
        if (StringUtils.isNotEmpty(url)) {
            CreateVideoModerationTaskRequest req = new CreateVideoModerationTaskRequest();
            req.setBizType(Constants.tencent_cloug_vm_biztype);
            req.setType(Constants.tencent_cloud_vm_type);
            TaskInput[] taskInputs = new TaskInput[1];
            TaskInput taskInput = new TaskInput();
            taskInput.setDataId(videoPrimaryKey);
            StorageInfo input = new StorageInfo();
            input.setType(Constants.tencent_cloud_vm_input_type);
            input.setUrl(url);
            taskInput.setInput(input);
            taskInputs[0] = taskInput;
            req.setTasks(taskInputs);
            try {
                response = getVmClient().CreateVideoModerationTask(req);
            } catch (TencentCloudSDKException e) {
                LOGGER.error("腾讯视频审核出错：", e);
            }
        }
        return response;
    }

    public DescribeTaskDetailResponse describeTaskDetail(String taskId) {
        DescribeTaskDetailResponse response = null;
        if (StringUtils.isNotEmpty(taskId)) {
            DescribeTaskDetailRequest req = new DescribeTaskDetailRequest();
            req.setTaskId(taskId);
            try {
                response = getVmClient().DescribeTaskDetail(req);
            } catch (TencentCloudSDKException e) {
                LOGGER.error("查询腾讯视频审核结果出错：", e);
            }
        }
        return response;
    }

    private VmClient getVmClient() {
        JsonObject vmConf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tencent_cloud_vm_conf), JsonObject.class);
        String secretId = vmConf.get(Constants.tencent_cloud_secret_id_name).getAsString();
        String secretKey = vmConf.get(Constants.tencent_cloud_secret_key_name).getAsString();
        Credential credential = new Credential(secretId, secretKey);
        VmClient vmClient = new VmClient(credential, Region.Guangzhou.getValue());
        return vmClient;
    }

    public static void main(String[] args) throws TencentCloudSDKException {

        /*String secretId = "AKIDKm0ymLnka9ZmmfOzUlthMm1GLAlARPDl";

        String secretKey = "yiOp0jGbrrkPtDIF3yoQwCDl47uRejZI";

        Credential credential = new Credential(secretId, secretKey);

        VmClient vmClient = new VmClient(credential, Region.Guangzhou.getValue());*/

        /*CreateVideoModerationTaskRequest req = new CreateVideoModerationTaskRequest();
        req.setBizType("default");
        req.setType("VIDEO");
        TaskInput[] taskInputs = new TaskInput[1];
        TaskInput taskInput = new TaskInput();
        taskInput.setDataId("XXX");
        StorageInfo input = new StorageInfo();
        input.setType("URL");
        input.setUrl("https://vodapp.hunancatv.com/d9ee1080vodcq1301401569/3374f2055285890810110576860/dGb3kNEtJXgA.mp4");
        taskInput.setInput(input);
        taskInputs[0] = taskInput;
        req.setTasks(taskInputs);
        CreateVideoModerationTaskResponse res = vmClient.CreateVideoModerationTask(req);
        if (res != null) {
        }*/

        /*DescribeTaskDetailRequest req = new DescribeTaskDetailRequest();
        req.setTaskId("task-video-X7c7QvGquSlOt0ue");
        DescribeTaskDetailResponse res = vmClient.DescribeTaskDetail(req);
        if (null != res) {
        }*/

    }

}
